nums = [-2, 3, -2, 4]
#超时
def maxProduct(nums):
    numsLen=len(nums)

    thenums=[[0 for _ in range(numsLen)] for _ in range(numsLen)]
    #第一行
    for i in range(numsLen):
        thenums[0][i]=nums[i]
    #剩余行元素
    for i in range(1,numsLen):
        for j in range(i,numsLen):
            thenums[i][j]=thenums[i-1][j-1]*nums[j]
    maxVal=thenums[0][0]
    for i in range(numsLen):
        for j in range(i,numsLen):
            if maxVal<thenums[i][j]:
                maxVal=thenums[i][j]
    print(thenums)
    return maxVal

def maxProduct1(nums):
    numsLen=len(nums)
    maxVal=nums[0]
    imax=1
    imin=1
    for i in range(numsLen):
        if nums[i]<0:
            temp=imax
            imax=imin
            imin=temp
        imax=max(imax*nums[i],nums[i])
        imin=min(imin*nums[i],nums[i])
        maxVal=max(maxVal,imax)
    return maxVal
print(maxProduct1(nums))